package me.zhengjie.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.pdf.make.MakePdf;
import me.zhengjie.pdf.vo.PDFVO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class Pdf {

    public static void main(String[] args) {
        MakePdf makePdf = new MakePdf();
        JSONObject freeLanceObject = new JSONObject();
        freeLanceObject.put("name", "张三");
        freeLanceObject.put("idcard", "220202198906063333");
        freeLanceObject.put("applicant", "李四");
        freeLanceObject.put("assesslv", "3级 重度失能Ⅲ级");
        freeLanceObject.put("sftg", "未达到");
        freeLanceObject.put("assessdate", "2025-04-09");
        freeLanceObject.put("C1_1", "1");
        freeLanceObject.put("C1_2", "2");
        freeLanceObject.put("C1_3", "3");
        freeLanceObject.put("C1_4", "4");
        freeLanceObject.put("C1_5", "5");
        freeLanceObject.put("C1_6", "6");
        freeLanceObject.put("C1_7", "7");
        freeLanceObject.put("C1_8", "8");
        freeLanceObject.put("C1_9", "9");
        freeLanceObject.put("C1_10", "10");
        freeLanceObject.put("C1", "55");
        freeLanceObject.put("C1_ASSESSOR_1", "刘鹏华");
        freeLanceObject.put("C1_ASSESSOR_2", "夏洪波");
        freeLanceObject.put("C2_11", "0");
        freeLanceObject.put("C2_12", "1");
        freeLanceObject.put("C2_13", "2");
        freeLanceObject.put("C2_14", "3");
        freeLanceObject.put("C2", "6");
        freeLanceObject.put("C2_ASSESSOR_1", "夏洪波");
        freeLanceObject.put("C2_ASSESSOR_2", "刘丽娜");
        freeLanceObject.put("C3_15", "1");
        freeLanceObject.put("C3_16", "2");
        freeLanceObject.put("C3_17", "4");
        freeLanceObject.put("C3", "7");
        freeLanceObject.put("C3_ASSESSOR_1", "郭建磊");
        freeLanceObject.put("C3_ASSESSOR_2", "王二");

        List<PDFVO> list = makePdf.getCoordinate(freeLanceObject);

        String datetime = cn.hutool.core.date.DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        String pdfName = datetime+".pdf";

        File fileDemo = new File("F:\\WPSCloud\\评估结论书和评估表.pdf");//样例文件 固定地址存放
        File fileFont = new File("C:\\eladmin\\file\\"+"songti.ttf");//字体 固定地址存放



        File fileTarget = new File("F:\\WPSCloud\\"+pdfName);//目标文件 地址可以直接创建在临时目录
        try{
            Files.copy(fileDemo.toPath(), fileTarget.toPath());//样例文件内容 复制到 目标文件
            PDDocument template = PDDocument.load(fileTarget);//对目标文件进行操作
            //遍历添加文本
            for (PDFVO pdfvo : list){
                PDPage page = template.getPage(pdfvo.getPage());
                PDPageContentStream contentStream = new PDPageContentStream(template, page, PDPageContentStream.AppendMode.APPEND, false, false);
                PDType0Font font = PDType0Font.load(template, fileFont);
                contentStream.beginText();
                contentStream.setFont(font, 14);
                contentStream.newLineAtOffset(pdfvo.getTx(), pdfvo.getTy());//位置
                contentStream.showText(pdfvo.getValue());//值
                contentStream.endText();
                contentStream.close();
            }
            //写入权限
            fileTarget.setWritable(true);
            // 保存PDF文件
            template.save(fileTarget);
            template.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
